package com.quzi.kettle.datapiece;

import android.util.Log;

import com.quzi.kettle.datapiece.status.DeviceStatus;
import com.quzi.kettle.utils.ByteUtils;

public class DataBodyDevRequest extends FactoryDataBody {

	public DataBodyDevRequest(DeviceStatus status) {
		setDataBodyStatus(status);
	}

	/**
	 * 第零个byte
	 */
	byte Zerobyte;

	/**
	 * 第一个byte
	 */
	byte FirstByte;
	byte switchStatus;// 开关状态
	byte runNingStatus;// 运行状态
	byte waterPumb;// 水泵
	byte timerMode;// 定时方式
	byte test1; // 快检
	byte connectMode;// 连接状态

	/**
	 * 第二个byte
	 */
	byte Secondbyte;

	byte setTemperature;// 设定温度

	/**
	 * 第三个byte
	 */
	byte Thirdbyte;

	byte setFixedTemperatureTime;// 设定保温
	byte get_rid_cl_work;// 除氯状态
	/**
	 * 第四个byte
	 */
	byte Fourthbyte;
	byte appointed_timer_on_aboso_min;// 预约开-绝对分
	byte appointed_timer_on; // 预约开

	/**
	 * 第五个byte
	 */
	byte Fifthbyte;
	byte appointed_timer_on_aboso_hour;// 预约开-绝对小时
	byte fixed_timer_on;// 定时开

	/**
	 * 第六个byte
	 */
	byte Sixthbyte;
	byte fixed_timer_on_aboso_min;// 定时开-绝对分
	byte mobile_timer; // 移动终端定时信息

	/**
	 * 第七个byte ？
	 */
	byte Seventhbyte;
	byte fixed_timer_on_aboso_hour;// 定时开-绝对小时

	/**
	 * 第八个byte
	 */
	byte Eighthbyte;
	byte Saturday; // 周六
	byte Friday; // 周五
	byte Thursday; // 周四
	byte Wednesday; // 周三
	byte Tuesday; // 周二
	byte Monday; // 周一
	byte Sunday; // 周日
	byte weekTimerLoop; // 周定时循环
	/**
	 * 第九个byte
	 */
	byte Ninethbyte;
	byte pumbAddWater;// 水泵加水量
	byte pumbCont; // 水泵控制
	byte beeWarning;// 蜂鸣器提醒

	/**
	 * 第十个byte
	 */
	byte Tenthbyte;
	byte lightCont; // 亮度控制
	byte contWay; // 控制方式
	byte lampCont; // 灯控制

	/**
	 * 第十一个byte
	 */
	byte Eleventhbyte;
	byte boilingTimer;// 沸腾时间控制
	byte get_rid_cl; // 除氯开关
	byte whatmode;

	@Override
	public byte[] toBytes() {

		byte[] bytes = new byte[13];
		Zerobyte = (byte) 0xEF;
		FirstByte = (byte) (0x00 | (switchStatus << 1 & 0x02)
				| (runNingStatus & 0x04) | (waterPumb & 0x08)
				| (timerMode & 0x10) | (test1 & 0x20) | (connectMode & 0x40 | 0x00));
		// setTemperature = 90;
		Secondbyte = (byte) (0x00 | setTemperature << 1);
		Thirdbyte = (byte) ((setFixedTemperatureTime & 0x7F) | ((get_rid_cl_work << 7) & 0x80));
		Log.d("mmqtt", "setFixedTemperatureTime = " + setFixedTemperatureTime
				+ "  get_rid_cl_work = " + get_rid_cl_work);
		Fourthbyte = (byte) ((appointed_timer_on_aboso_min & 0x3F)
				| (appointed_timer_on << 6 & 0x40) | 0x00);
		Fifthbyte = (byte) (0x00 | (appointed_timer_on_aboso_hour << 1 & 0x3E)
				| (fixed_timer_on & 0x40) | 0x00);
		Sixthbyte = (byte) ((fixed_timer_on_aboso_min & 0x3F)
				| (mobile_timer & 0x40) | 0x00);
		Seventhbyte = (byte) ((fixed_timer_on_aboso_hour & 0x3F) | 0x00);
		Eighthbyte = (byte) ((Saturday & 0x01) | (Friday & 0x02)
				| (Thursday & 0x04) | (Wednesday & 0x08) | (Tuesday & 0x10)
				| (Monday & 0x20) | (Sunday & 0x40) | (weekTimerLoop & 0x80));
		Ninethbyte = (byte) (0x00 | (pumbAddWater << 1 & 0x1E)
				| (pumbCont & 0x20) | ((beeWarning << 6) & 0x40) | 0x00);
		Tenthbyte = (byte) (0x00 | (lightCont << 1 & 0x1E) | (contWay & 0x20)
				| (lampCont & 0x40) | 0x00);
		Eleventhbyte = (byte) (0x00 | (boilingTimer << 1 & 0x3E)
				| ((get_rid_cl << 5) & 0x20) | ((whatmode << 6) & 0xC0));

		Log.d("mmqtt", "除氯 " + get_rid_cl + "   除氯时间：" + boilingTimer);

		setBytes(bytes);

		byte[] b = addHead(bytes);
		return b;
	}

	private void setBytes(byte[] bytes) {
		bytes[0] = Zerobyte;
		bytes[1] = FirstByte;
		bytes[2] = Secondbyte;
		bytes[3] = Thirdbyte;
		bytes[4] = Fourthbyte;
		bytes[5] = Fifthbyte;
		bytes[6] = Sixthbyte;
		bytes[7] = Seventhbyte;
		bytes[8] = Eighthbyte;
		bytes[9] = Ninethbyte;
		bytes[10] = Tenthbyte;
		bytes[11] = Eleventhbyte;
		bytes[12] = getCRC(bytes.clone(), bytes.length - 2);

	}

	public void setDataBodyStatus(DeviceStatus newStatus) {

		switchStatus = newStatus.switchStatus;
		runNingStatus = newStatus.runNingStatus;
		waterPumb = newStatus.waterPumb;
		timerMode = newStatus.timerMode;
		test1 = newStatus.test1;
		connectMode = newStatus.connectMode;

		setTemperature = newStatus.setTemperature;

		setFixedTemperatureTime = newStatus.setFixedTemperatureTime;
		get_rid_cl_work = newStatus.get_rid_cl_work;

		appointed_timer_on_aboso_min = newStatus.appointed_timer_on_aboso_min;
		appointed_timer_on = newStatus.appointed_timer_on;

		appointed_timer_on_aboso_hour = newStatus.appointed_timer_on_aboso_hour;
		fixed_timer_on = newStatus.fixed_timer_on;

		fixed_timer_on_aboso_min = newStatus.fixed_timer_on_aboso_min;
		mobile_timer = newStatus.mobile_timer;

		fixed_timer_on_aboso_hour = newStatus.fixed_timer_on_aboso_hour;

		Saturday = newStatus.Saturday;
		Friday = newStatus.Friday;
		Thursday = newStatus.Thursday;
		Wednesday = newStatus.Wednesday;
		Tuesday = newStatus.Tuesday;
		Monday = newStatus.Monday;
		Sunday = newStatus.Sunday;
		weekTimerLoop = newStatus.weekTimerLoop;

		pumbAddWater = newStatus.pumbAddWater;
		pumbCont = newStatus.pumbCont;
		beeWarning = newStatus.beeWarning;

		lightCont = newStatus.lightCont;
		contWay = newStatus.contWay;
		lampCont = newStatus.lampCont;

		boilingTimer = newStatus.boilingTimer;
		get_rid_cl = newStatus.get_rid_cl;
		setFixedTemperatureTime = newStatus.setFixedTemperatureTime;
		get_rid_cl_work = newStatus.get_rid_cl_work;
		whatmode = newStatus.whatmode;
	}

	@Override
	public DeviceStatus toObject(byte[] bytes) {
		// TODO Auto-generated method stub
		return null;
	}

}
